#include "vas_config.h"

namespace vas {
    VasConfig ReadConfig(const std::string &config_str) {
        Json::Value root = StringToJson(config_str);
        VasConfig config;
        config.http_port = root["http_port"].asInt();
        config.http_threads = root["http_threads"].asInt();
        config.algorithm_threads = root["algorithm_threads"].asInt();
        config.max_videos = root["max_videos"].asInt();
        config.rocket_mq_host = root["rocket_mq_host"].asString();
        config.zookeeper_host = root["zookeeper_host"].asString();
        for (auto i : root["algorithms"]) {
            config.algorithm_mock.insert(std::pair<std::string, bool>(i["name"].asString(), i["mock"].asBool()));
        }
        for (const auto &i :root["local_channels"]) {
            Channel channel;
            channel.channel = i["channel"].asString();
            channel.url = i["url"].asString();
            for (const auto &j: i["tasks"]) {
                Task task;
                task.type = j["type"].asString();
                task.interval = j["interval"].asInt();
                for (const auto &k : j["roi"]) {
                    Coord coord{};
                    coord.x = k[0].asInt();
                    coord.y = k[1].asInt();
                    task.roi.emplace_back(coord);
                }
                channel.tasks.emplace_back(task);
            }
            config.local_channels.emplace_back(JsonToChannel(i));
        }
        return config;
    }

    Channel JsonToChannel(Json::Value json) {
        Channel channel;
        channel.channel = json["channel"].asString();
        channel.url = json["url"].asString();
        for (const auto &j: json["tasks"]) {
            Task task;
            task.type = j["type"].asString();
            task.interval = j["interval"].asInt();
            for (const auto &k : j["roi"]) {
                Coord coord{};
                coord.x = k[0].asInt();
                coord.y = k[1].asInt();
                task.roi.emplace_back(coord);
            }
            channel.tasks.emplace_back(task);
        }
        return channel;
    }

}

